﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;

namespace Zhongy
{
    public class AnswerHandler : IHandler
    {
        private static MsgRecord msgRecord = Record.MsgRecord;

        public override void OnDisconnect(ClientPer client)
        {

        }

        public override void OnReceive(ClientPer client, SocketReceiveMsg msg)
        {
            switch (msg.MT)
            {
                case OpCode.TYPE_DEBUG_MSG_C:
                    Client2GameDebug(client, msg);
                    break;

                case OpCode.TYPE_ACK2G:
                    Analy(client, msg);
                    break;

                default:
                    Debug.LogError("信息归属不对");
                    break;
            }
        }

        void Client2GameDebug(ClientPer client, SocketReceiveMsg msg)
        {
#if UNITY_EDITOR
            Debug.LogError
#else
            Debug.Log
#endif
                (string.Format("<color=red>收到来自client的debug信息:{0}</color>", msg.Msg));
        }

        void Analy(ClientPer client, SocketReceiveMsg msg)
        {
            if (msg == null || string.IsNullOrEmpty(msg.ackid))
            {
                Debug.LogError("数据有误或ackid为空");
                return;
            }

            if (!msgRecord.IsExsit(msg.ackid))
            {
                Debug.Log("<color=red>返回消息可能超时</color>");
                return;
            }

            SocketSendMsg sendMsg = msgRecord.Get(msg.ackid);

            switch (sendMsg.MT)
            {
                case OpCode.TYPE_STOP_GAME_G:
                    ClientRespondGameExit(client, msg);
                    break;

                case OpCode.TYPE_GAME_DISCONNECT:
                    ClientRespondGameDisconnect(client, msg);
                    break;

                default:
                    break;
            }
        }

        void ClientRespondGameExit(ClientPer client, SocketReceiveMsg msg)
        {
            if (msg == null)
                Debug.LogError("数据无效");

            if (msg.result.Equals("OK",StringComparison.CurrentCultureIgnoreCase))
            {
                Debug.Log(string.Format("<color=red>client同意游戏结束请求，详细信息:{0}</color>", msg.mesg));

                //todo 是否需要client端确定游戏端退出 GameManager.Game2ClientStop
                //Dispatch(TransCode.Net,GameEvtCode.GameConnect);
            }
            else if (msg.result.Equals("DENIED", StringComparison.CurrentCultureIgnoreCase))
            {
                Debug.Log(string.Format("<color=red>client拒绝游戏结束请求，详细信息:{0}</color>", msg.mesg));
            }
            else if (msg.result.Equals("ERROR", StringComparison.CurrentCultureIgnoreCase))
            {
                Debug.Log(string.Format("<color=red>client响应游戏结束出错，详细信息:{0}</color>", msg.mesg));
            }

        }

        void ClientRespondGameDisconnect(ClientPer client, SocketReceiveMsg msg)
        {

            if (msg == null)
                Debug.LogError("数据无效");

            if (msg.result.Equals("OK", StringComparison.CurrentCultureIgnoreCase))
            {
                Debug.Log(string.Format("<color=red>client同意游戏断连接请求，详细信息:{0}</color>", msg.mesg));
                //todo 是否需要client端确定游戏端断连 GameManager.Game2ClientDisconnect
                //Dispatch(TransCode.Net, GameEvtCode.GameConnect);
            }
            else if (msg.result.Equals("DENIED", StringComparison.CurrentCultureIgnoreCase))
            {
                Debug.Log(string.Format("<color=red>client同意游戏断连接请求，详细信息:{0}</color>", msg.mesg));
            }
           else if (msg.result.Equals("ERROR", StringComparison.CurrentCultureIgnoreCase))
            {
                Debug.Log(string.Format("<color=red>client同意游戏断连接请求，详细信息:{0}</color>", msg.mesg));
            }
        }
    }
}
